﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;
using DataContext;

namespace QLCF.Forms
{
    public partial class frmStock : Form
    {
        public Boolean isAddData = false;
        private bool isLoading = true;
        public int m_stockID = -1;

        public int permission = 0;

        private DateTime FromDate = DateTime.Now;
        private DateTime ToDate = DateTime.Now;
        private int AdminID=-1;

        public frmStock()
        {
            InitializeComponent(); 
            bindAdmin();
            permission = CtrAdmin.GetPermission(Program.UserEntity.Permission, Config.Enum.Function.frmStock);

            if ((permission & (int)Config.Enum.Permission.Add) != (int)Config.Enum.Permission.Add)
            {
                tsbAdd.Enabled = false;
                toolStripMenuItemAdd.Enabled = false;
            }

            if ((permission & (int)Config.Enum.Permission.Edit) != (int)Config.Enum.Permission.Edit)
            {
                tsbEdit.Enabled = false;
                toolStripMenuItemEdit.Enabled = false;
            }

            if ((permission & (int)Config.Enum.Permission.Delete) != (int)Config.Enum.Permission.Delete)
            {
                tsbDelete.Enabled = false;
                toolStripMenuItemDelete.Enabled = false;
            }
        }

        private void frmStock_Load(object sender, EventArgs e)
        {
            this.isLoading = false;
            splitContainer3.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
            splitContainer3.Location = new Point(0, 10); splitContainer3.Size = new Size(splitContainer3.Parent.ClientSize.Width, splitContainer3.Parent.ClientSize.Height - (0 + 10));            
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            this.isAddData = true;
            frmStockDetail frmStockDetail = new frmStockDetail(this);
            frmStockDetail.ShowDialog();
        }        

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            tsbAdd.PerformClick();
        }

        private void grvStock_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = grvStock.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    grvStock.ClearSelection();
                    grvStock.CurrentCell = grvStock.Rows[hti.RowIndex].Cells[hti.ColumnIndex];
                    grvStock.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Selected = true;
                    grvStock.ContextMenuStrip = contextMenuStripOnProduct;
                    contextMenuStripOnProduct.Items["toolStripMenuItemEdit"].Enabled = true;
                    contextMenuStripOnProduct.Items["toolStripMenuItemRefresh"].Enabled = true;
                    contextMenuStripOnProduct.Items["toolStripMenuItemDelete"].Enabled = true;
                }
                else
                {
                    contextMenuStripOnProduct.Items["toolStripMenuItemEdit"].Enabled = false;
                    contextMenuStripOnProduct.Items["toolStripMenuItemRefresh"].Enabled = true;
                    contextMenuStripOnProduct.Items["toolStripMenuItemDelete"].Enabled = false;
                }
            }
        }

        public void BindStock()
        {                     
            CtrBase ctrBase = new CtrBase();
            int? totalMoney = 0;
            var lstStock = ctrBase.GetAllStockSearch(FromDate, ToDate, AdminID, ref totalMoney);
            lblTotalMoneyAll.Text = (totalMoney ?? 0) == 0 ? "0 VNĐ" : (totalMoney ?? 0).ToString("0,0") + " VNĐ";
            if (lstStock.Count > 0)
            {
                grvStock.AutoGenerateColumns = false;
                grvStock.DataSource = lstStock;
                this.isLoading = false;
                grvStock.Select();                
            }
            else
            {
                grvStock.DataSource = null;
            }            
        }

        public void BindStockDetail()
        {
            grvStockDetail.DataSource = null;
            lblTotalMoneyDetail.Text = "0 VNĐ";
        }
        
        private void grvStock_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                SolidBrush b = new SolidBrush(grvStock.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), grvStock.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }
            catch { }
        }
        
        private void grvStock_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.isLoading) return;
            var row = e.RowIndex;
            m_stockID = Convert.ToInt32(grvStock.Rows[row].Cells["StockID"].Value);

            CtrBase ctrBase = new CtrBase();
            int? totalMoney = 0;
            var lstStockDetail = ctrBase.StockDetailGetList(m_stockID, ref totalMoney);
            lblTotalMoneyDetail.Text = (totalMoney ?? 0) == 0 ? "0 VNĐ" : (totalMoney ?? 0).ToString("0,0") + " VNĐ";
            if (lstStockDetail.Count > 0)
            {
                grvStockDetail.AutoGenerateColumns = false;
                grvStockDetail.DataSource = lstStockDetail;
                this.isLoading = false;                
            }
            else
            {
                grvStockDetail.DataSource = null;
            }
        }

        private void grvStockDetail_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                SolidBrush b = new SolidBrush(grvStockDetail.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), grvStockDetail.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }
            catch { }
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (this.m_stockID == -1)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show("Bạn chắc chắn xoá bản ghi này không?", "Thông báo...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            CtrBase ctrBase = new CtrBase();
            var objDelete =ctrBase.StockDelete(this.m_stockID);
            if (objDelete > 0)
            {
                CtrAdmin ctra = new CtrAdmin();
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 3, "Xóa kho hàng có mã \"" + objDelete + "\"", "Quản lý nhập kho");
                BindStock();
                BindStockDetail();
            }
        }

        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            tsbDelete.PerformClick();
        }

        private void bindAdmin()
        {
            CtrAdmin ctra = new CtrAdmin();
            cbxListUser.ValueMember = "AdminID";
            cbxListUser.DisplayMember = "FullName";
            List<uspAdminGetListAllResult> items = ctra.GetListAdminAll();
            uspAdminGetListAllResult all = new uspAdminGetListAllResult();
            all.AdminID = -1;
            all.UserName = "Tất cả";
            all.FullName = "Tất cả";
            items.Insert(0, all);
            cbxListUser.DataSource = items;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CtrBase ctrBase = new CtrBase();
            FromDate = dateTimePicker1.Value;
            ToDate = dateTimePicker2.Value;
            AdminID=Convert.ToInt32(cbxListUser.SelectedValue);
            BindStock();

        }

        private void tsbEdit_Click(object sender, EventArgs e)        
        {
            if (this.m_stockID == -1)
            { 
                MessageBox.Show("Bạn chưa chọn bản ghi chỉnh sửa!", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            this.isAddData = false;
            frmStockDetail frmStockDetail = new frmStockDetail(this);
            frmStockDetail.ShowDialog();
        }

        private void toolStripMenuItemEdit_Click(object sender, EventArgs e)
        {
            tsbEdit.PerformClick();
        }

        private void btnPrintReportStock_Click(object sender, EventArgs e)
        {
            Reports.frmViewlstStockReport rptS = new Reports.frmViewlstStockReport(FromDate, ToDate, AdminID);
            rptS.ShowDialog();
        }

        private void btnPrintBillStock_Click(object sender, EventArgs e)
        {
            if (m_stockID <= 0) return;
            Reports.frmViewStockDetail frmSD = new Reports.frmViewStockDetail(m_stockID);
            frmSD.ShowDialog();
        }

        private void grvStock_DoubleClick(object sender, EventArgs e)
        {
            tsbEdit.PerformClick();
        }
    }
}
